//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#if 0
#include <buola/image.h>
#include <buola/image/cimageplugin.h>

#include <buola/image/io_jpeg.h>

extern "C"
{
    #include <gif_lib.h>
}

namespace buola {

class CGIFCodec : public CImageCodec 
{
public:
    CGIFCodec();
    virtual ~CGIFCodec();

    virtual void ReadImage(img::CImage_rgb8 &pData,io::PBufferStream pStream,
                        const CSize_i &pSize);
    virtual void WriteImage(const img::CView_rgb8 &pView,io::PBufferStream pStream);

protected:
    GifFileType *mGifFile;
};

class CGIFPlugin : public CImagePlugin
{
public:
    CGIFPlugin()
    {
        RegisterPlugin("gif",this);
        RegisterPlugin("GIF",this);
    }
    
    CImageCodec *GetInstance()
    {
        return new CGIFCodec;
    }
};

static CGIFPlugin sGIFPlugin;

CGIFCodec::CGIFCodec()
{
    mGifFile=nullptr;
}

CGIFCodec::~CGIFCodec()
{
    if(mGifFile)
        DGifCloseFile(mGifFile);
}

void CGIFCodec::ReadImage(img::CImage_rgb8 &pData,io::PBufferStream pStream,
                                const CSize_i &pSize)
{
    mGifFile=DGifOpenFileHandle((DCast<COldBuolaDiskStream*>(pStream))->GetFD());
    
    if(!mGifFile)
    {
        return nullptr;
    }

    DGifSlurp(mGifFile);

    unsigned char *lBitmap8=(unsigned char*)mGifFile->SavedImages[0].RasterBits;
    
    CPalette *lPalette=new CPalette;
    
    for(int i=0;i<mGifFile->SColorMap->ColorCount;i++)
    {
        lPalette->c[i]=RGB(mGifFile->SColorMap->Colors[i].Red,
                      mGifFile->SColorMap->Colors[i].Green,
                      mGifFile->SColorMap->Colors[i].Blue);
    }

    int lBitmapSize=mGifFile->SHeight*mGifFile->SWidth;
    
    unsigned char *lBitmap=alloc_array<unsigned char>(lBitmapSize*4);

    ConvertBitmap8To32(lBitmap8,lBitmap,lPalette->c,lBitmapSize);

    PImage lImage=new CImage;
    lImage->Create(lBitmap,nullptr,CSize_d(mGifFile->SWidth,mGifFile->SHeight));
    lImage->SetPalette(lPalette);
    lImage->SetColorMask(CColor(lPalette->c[255]));
    
    return lImage;
}

void CGIFCodec::WriteImage(const img::CView_rgb8&,io::PBufferStream)
{
}

/*namespace buola*/ }

#endif


